package com.group.three.controller;

import com.group.three.pojo.PreviewVideos;
import com.group.three.pojo.dto.AddNewPreviewVideoDto;
import com.group.three.pojo.vo.JsonResult;
import com.group.three.service.PreviewVideosService;
import com.group.three.utils.SnowFlakeUtil;
import com.group.three.utils.TimeUtils;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.time.LocalDateTime;
import java.util.*;

@Controller
@RequestMapping("/admin")
public class PreviewVideosController {
    @Autowired
    private PreviewVideosService previewVideosService;

    @RequestMapping("/preview")
    public ModelAndView modelAndView(ModelAndView modelAndView) {
        List<PreviewVideos> previewVideos = previewVideosService.selectPreviewVideosAll();
        modelAndView.addObject("previewVideos", previewVideos);
        modelAndView.setViewName("/3-admin/preview_list");
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/deletePreview")
    public JsonResult preview(Long previewId) {
        int deletePreviewId = previewVideosService.deletePreviewId(previewId);
        if (deletePreviewId <= 0) {
            JsonResult jsonResult = new JsonResult("0", "失败");
            return jsonResult;
        }
        JsonResult jsonResult = new JsonResult("200", "成功");
        return jsonResult;
    }

    @ResponseBody
    @RequestMapping("/searchPreview")
    public JsonResult searchByInput(String input) {
        List<PreviewVideos> previewVideos = previewVideosService.searchByInput(input);
        return new JsonResult("200", "success", previewVideos);
    }
    //=========================================添加预告片=======================
    @RequestMapping("/goAddPreviewVideo")
    public ModelAndView goAddPreviewVideo(ModelAndView modelAndView){


        modelAndView.setViewName("/3-admin/preview_add");
        return modelAndView;
    }

    /**
     * 封面上传
     * @param uploadFile
     * @param session
     * @return
     */
    @SneakyThrows
    @RequestMapping("/uploadImage")
    @ResponseBody
    public JsonResult uploadImage(MultipartFile uploadFile, HttpSession session){
        String fileName = uploadFile.getOriginalFilename();
        String hzName = fileName.substring(fileName.lastIndexOf("."));
        String fileUUid = UUID.randomUUID().toString();
        fileName =  fileUUid+ hzName;
        ServletContext servletContext = session.getServletContext();
        String localPath = "/resource/main_banner";
        String filePath = servletContext.getRealPath(localPath);
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdir();
        }
        String finalPath = filePath + File.separator + fileName;
        file = new File(finalPath);
        uploadFile.transferTo(file);
        Map<String, String> data = new HashMap<>();
        data.put("src", localPath + "\\" + fileName);
        data.put("fileName", fileName);
        return new JsonResult("200","success",data);
    }

    @RequestMapping("/addPreview")
    @ResponseBody
    public JsonResult addPreview(AddNewPreviewVideoDto addNewPreviewVideoDto){
        addNewPreviewVideoDto.setUpload_date(LocalDateTime.now().toString());
        addNewPreviewVideoDto.setPreviewVideId(SnowFlakeUtil.getSnowFlakeId());

        boolean b = previewVideosService.AddNewPreviewVideo(addNewPreviewVideoDto);
        if (b){
            return new JsonResult("200","success");
        }else {
            return new JsonResult("400","error");
        }



    }
}
